package cn.stylefeng.guns.modular.working.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.working.entity.Working;
import cn.stylefeng.guns.modular.working.mapper.WorkingMapper;
import cn.stylefeng.guns.modular.working.model.params.WorkingParam;
import cn.stylefeng.guns.modular.working.model.result.WorkingResult;
import  cn.stylefeng.guns.modular.working.service.WorkingService;
import cn.stylefeng.guns.wrapper.MyWrapper;
import cn.stylefeng.roses.core.datascope.DataScope;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author syt
 * @since 2020-10-11
 */
@Service
public class WorkingServiceImpl extends ServiceImpl<WorkingMapper, Working> implements WorkingService {

    @Override
    public void add(WorkingParam param){
        Working entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(WorkingParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(WorkingParam param){
        Working oldEntity = getOldEntity(param);
        Working newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public WorkingResult findBySpec(WorkingParam param){
        return null;
    }

    @Override
    public List<WorkingResult> findListBySpec(WorkingParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(WorkingParam param){
        Page pageContext = getPageContext();
        DataScope dataScope = new DataScope(LoginContextHolder.getContext().getDeptDataScope());
        Page<Map<String, Object>> page = this.baseMapper.customPageMapList(dataScope, pageContext, param);
        Page<Map<String, Object>> wrap = new MyWrapper(page).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    private Serializable getKey(WorkingParam param){
        return param.getWorkingId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Working getOldEntity(WorkingParam param) {
        return this.getById(getKey(param));
    }

    private Working getEntity(WorkingParam param) {
        Working entity = new Working();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }


}
